home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 04 class fundamentals / objectpool / form1.vb < prev    next >
Encoding:
Text File  |  2001-07-19  |  8.6 KB  |  228 lines

  1. Public Class Form1
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.         Friend WithEvents Label1 As System.Windows.Forms.Label
  26.         Friend WithEvents Label2 As System.Windows.Forms.Label
  27.     Friend WithEvents btnStandard As System.Windows.Forms.Button
  28.     Friend WithEvents txtObjects As System.Windows.Forms.TextBox
  29.     Friend WithEvents txtIterations As System.Windows.Forms.TextBox
  30.     Friend WithEvents btnPooled As System.Windows.Forms.Button
  31.     Friend WithEvents lblStandard As System.Windows.Forms.Label
  32.     Friend WithEvents lblPooled As System.Windows.Forms.Label
  33.                 
  34.     'Required by the Windows Form Designer
  35.     Private components As System.ComponentModel.Container
  36.  
  37.     'NOTE: The following procedure is required by the Windows Form Designer
  38.     'It can be modified using the Windows Form Designer.  
  39.     'Do not modify it using the code editor.
  40.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  41.         Me.txtObjects = New System.Windows.Forms.TextBox()
  42.         Me.txtIterations = New System.Windows.Forms.TextBox()
  43.         Me.Label1 = New System.Windows.Forms.Label()
  44.         Me.Label2 = New System.Windows.Forms.Label()
  45.         Me.lblStandard = New System.Windows.Forms.Label()
  46.         Me.btnStandard = New System.Windows.Forms.Button()
  47.         Me.btnPooled = New System.Windows.Forms.Button()
  48.         Me.lblPooled = New System.Windows.Forms.Label()
  49.         Me.SuspendLayout()
  50.         '
  51.         'txtObjects
  52.         '
  53.         Me.txtObjects.Location = New System.Drawing.Point(208, 16)
  54.         Me.txtObjects.Name = "txtObjects"
  55.         Me.txtObjects.Size = New System.Drawing.Size(72, 26)
  56.         Me.txtObjects.TabIndex = 2
  57.         Me.txtObjects.Text = "10"
  58.         '
  59.         'txtIterations
  60.         '
  61.         Me.txtIterations.Location = New System.Drawing.Point(208, 64)
  62.         Me.txtIterations.Name = "txtIterations"
  63.         Me.txtIterations.Size = New System.Drawing.Size(72, 26)
  64.         Me.txtIterations.TabIndex = 2
  65.         Me.txtIterations.Text = "100"
  66.         '
  67.         'Label1
  68.         '
  69.         Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  70.         Me.Label1.Location = New System.Drawing.Point(32, 16)
  71.         Me.Label1.Name = "Label1"
  72.         Me.Label1.Size = New System.Drawing.Size(144, 24)
  73.         Me.Label1.TabIndex = 1
  74.         Me.Label1.Text = "max # of objects"
  75.         Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
  76.         '
  77.         'Label2
  78.         '
  79.         Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  80.         Me.Label2.Location = New System.Drawing.Point(32, 64)
  81.         Me.Label2.Name = "Label2"
  82.         Me.Label2.Size = New System.Drawing.Size(144, 24)
  83.         Me.Label2.TabIndex = 1
  84.         Me.Label2.Text = "# of iterations"
  85.         Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
  86.         '
  87.         'lblStandard
  88.         '
  89.         Me.lblStandard.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  90.         Me.lblStandard.Cursor = System.Windows.Forms.Cursors.No
  91.         Me.lblStandard.Location = New System.Drawing.Point(208, 120)
  92.         Me.lblStandard.Name = "lblStandard"
  93.         Me.lblStandard.Size = New System.Drawing.Size(200, 40)
  94.         Me.lblStandard.TabIndex = 3
  95.         '
  96.         'btnStandard
  97.         '
  98.         Me.btnStandard.Location = New System.Drawing.Point(32, 120)
  99.         Me.btnStandard.Name = "btnStandard"
  100.         Me.btnStandard.Size = New System.Drawing.Size(152, 40)
  101.         Me.btnStandard.TabIndex = 0
  102.         Me.btnStandard.Text = "standard GCs"
  103.         '
  104.         'btnPooled
  105.         '
  106.         Me.btnPooled.Location = New System.Drawing.Point(32, 176)
  107.         Me.btnPooled.Name = "btnPooled"
  108.         Me.btnPooled.Size = New System.Drawing.Size(152, 40)
  109.         Me.btnPooled.TabIndex = 0
  110.         Me.btnPooled.Text = "Pooled objects"
  111.         '
  112.         'lblPooled
  113.         '
  114.         Me.lblPooled.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  115.         Me.lblPooled.Cursor = System.Windows.Forms.Cursors.No
  116.         Me.lblPooled.Location = New System.Drawing.Point(208, 176)
  117.         Me.lblPooled.Name = "lblPooled"
  118.         Me.lblPooled.Size = New System.Drawing.Size(200, 40)
  119.         Me.lblPooled.TabIndex = 3
  120.         '
  121.         'Form1
  122.         '
  123.         Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
  124.         Me.ClientSize = New System.Drawing.Size(488, 261)
  125.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblPooled, Me.lblStandard, Me.btnPooled, Me.Label2, Me.txtIterations, Me.txtObjects, Me.Label1, Me.btnStandard})
  126.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  127.         Me.Name = "Form1"
  128.         Me.Text = "Object Pool Demo"
  129.         Me.ResumeLayout(False)
  130.  
  131.     End Sub
  132.  
  133. #End Region
  134.  
  135.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStandard.Click
  136.         Dim i As Integer, j As Integer
  137.         Dim rand As New Random()
  138.         Dim objCount As Integer
  139.         Dim start As Date
  140.  
  141.         ' max number of objects
  142.         Dim objMax As Integer = CInt(txtObjects.Text)
  143.  
  144.         ' create an array that holds these objects
  145.         Dim arrObj(objMax) As Object
  146.  
  147.         start = Now
  148.  
  149.         ' repeat for the number of iterations specified
  150.         For i = 1 To CInt(txtIterations.Text)
  151.  
  152.             ' get the next number of objects that should go into the array
  153.             Dim objNextCount As Integer = rand.Next(0, objMax)
  154.  
  155.             If objNextCount > objCount Then
  156.                 ' we must create a few objects
  157.                 For j = objCount + 1 To objNextCount
  158.                     arrObj(j) = New RandomArray()
  159.                 Next
  160.             ElseIf objNextCount < objCount Then
  161.                 ' else we must release one or more objects
  162.                 For j = objNextCount + 1 To objCount
  163.                     arrObj(j) = Nothing
  164.                 Next
  165.             End If
  166.             objCount = objNextCount
  167.         Next
  168.  
  169.         ' clear the array and force a garbage collection
  170.         arrObj = Nothing
  171.         GC.Collect()
  172.  
  173.         ' display total time
  174.         lblStandard.Text = Now.Subtract(start).ToString
  175.  
  176.     End Sub
  177.  
  178.     Private Sub btnPooled_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPooled.Click
  179.         Dim i As Integer, j As Integer
  180.         Dim rand As New Random()
  181.         Dim objCount As Integer
  182.         Dim start As Date
  183.  
  184.         ' max number of objects
  185.         Dim objMax As Integer = CInt(txtObjects.Text)
  186.  
  187.         ' create an array that holds these objects
  188.         Dim arrObj(objMax) As Object
  189.  
  190.         start = Now
  191.  
  192.         ' create the poolmanager
  193.         Dim PoolMan As New PoolManager()
  194.  
  195.         ' repeat for the number of iterations specified
  196.         For i = 1 To CInt(txtIterations.Text)
  197.  
  198.             ' get the next number of objects that should go into the array
  199.             Dim objNextCount As Integer = rand.Next(0, objMax)
  200.  
  201.             If objNextCount > objCount Then
  202.                 ' we must create a few objects
  203.                 For j = objCount + 1 To objNextCount
  204.                     arrObj(j) = PoolMan.NewRandomArray
  205.                 Next
  206.             ElseIf objNextCount < objCount Then
  207.                 ' else we must release one or more objects
  208.                 For j = objNextCount + 1 To objCount
  209.                     arrObj(j) = Nothing
  210.                 Next
  211.             End If
  212.             ' remember current objCount
  213.             objCount = objNextCount
  214.         Next
  215.  
  216.         ' clear the array and force a garbage collection
  217.         arrObj = Nothing
  218.  
  219.         PoolMan.Dispose()
  220.         PoolMan = Nothing
  221.         GC.Collect()
  222.  
  223.         ' display total time
  224.         lblPooled.Text = Now.Subtract(start).ToString
  225.  
  226.     End Sub
  227. End Class
  228.